package pbx.service;

import java.util.List;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import pbx.model.Pbx;
import pbx.model.Queue;


@Stateless
public class QueueService {
	
	@PersistenceContext(unitName = "persistence")
    private EntityManager em;
	
	
	public Queue getById(Integer id){	
		return em.find(Queue.class, id);
	}
    
	public Queue save(Queue queue) {
		em.persist(queue);
		return queue;   
	}
	
	public Queue update(Queue queue) {
		return em.merge(queue);
	}
	
    public void delete(Integer id) {
    	em.remove(em.find(Queue.class, id));	
    }
    
	@SuppressWarnings("unchecked")
    public List<Queue> listByPbx(Pbx pbx) {
		List<Queue> result = null;
		Query query = em.createQuery("SELECT e FROM Queue e WHERE e.pbx = :pbx");
		query.setParameter("pbx", pbx);
		try {
			result = (List<Queue>) query.getResultList(); 
        }
    	catch (Exception e) {
			e.printStackTrace();
		}
        return result;
        
    }
	
	
	
}

